package com.elephantgreen.common;

import java.util.ArrayList;
import java.util.Date;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * Store details of new sales
 * @author Bishan
 * 
 */
public class NewSalesData extends Sale {

	
	private ArrayList<SaleProducts> saleProducts;
	
	SaleProducts sp;
	

	/**
	 * Constructor to Create new NewSale object.
	 * 
	 * @param invoiceNumber Invoice Number issued to Client
	 * @param client Client of sale
	 * @param saleDate Date of sale
	 * @param cashDiscount Given cash Discount to sale
	 * @param percentDiscount Given Percent Discount to sale
	 * @param saleTotal Total of sale before Discounts and Adjustments
	 * @param finalSaleAmount Total of sale after Discounts and Adjustments 
	 * @param adjustmentReason Detail about adjustment
	 * @param adjustmentAmount Adjustment Amount 
	 * @param comments comments about sale
	 * @param saleProducts Details of sold products
	 */
	public NewSalesData(String invoiceNumber, Client client, Date saleDate,
			double cashDiscount, int percentDiscount, double saleTotal,
			double finalSaleAmount, String adjustmentReason,
			double adjustmentAmount, String comments, ArrayList<SaleProducts> saleProducts) {
		super(invoiceNumber, client, saleDate, cashDiscount, percentDiscount,
				saleTotal, finalSaleAmount, adjustmentReason, adjustmentAmount,
				comments);
		this.saleProducts = saleProducts;
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see com.elephantgreen.common.EgCommon#createXml()
	 */
	@Override
	public Document createXml() {
		
		Document doc = null;

		try {

			DocumentBuilderFactory docFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
			doc = docBuilder.newDocument();

			// root element
			Element rootElement = doc.createElement("saleDetails");
			doc.appendChild(rootElement);

			// sale element
			Element sale = doc.createElement("sale");
			rootElement.appendChild(sale);

			// saleProducts element
			Element saleProducts = doc.createElement("saleProducts");
			rootElement.appendChild(saleProducts);

			// invoiceNumber element
			Element invoiceNumber = doc.createElement("invoiceNumber");
			invoiceNumber.appendChild(doc.createTextNode(getInvoiceNumber()));
			sale.appendChild(invoiceNumber);


			// clientId element
			Element clientId = doc.createElement("clientId");
			clientId.appendChild(doc.createTextNode(getClient().getClientId()));
			sale.appendChild(clientId);

			// saleDate element
			Element saleDate = doc.createElement("saleDate");
			saleDate.appendChild(doc.createTextNode(getSaleDate().toString()));
			sale.appendChild(saleDate);

			// cashDiscount element
			Element cashDiscount = doc.createElement("cashDiscount");
			cashDiscount.appendChild(doc.createTextNode(getCashDiscount()+""));
			sale.appendChild(cashDiscount);

			// percentDiscount element
			Element percentDiscount = doc.createElement("percentDiscount");
			percentDiscount.appendChild(doc
					.createTextNode(getPercentDiscount()+""));
			sale.appendChild(percentDiscount);

			// saleTotal element
			Element saleTotal = doc.createElement("saleTotal");
			saleTotal.appendChild(doc.createTextNode(getSaleTotal()+""));
			sale.appendChild(saleTotal);

			// finalSaleAmount element
			Element finalSaleAmount = doc.createElement("finalSaleAmount");
			finalSaleAmount.appendChild(doc
					.createTextNode(getFinalSaleAmount()+""));
			sale.appendChild(finalSaleAmount);

			// adjustmentReason element
			Element adjustmentReason = doc.createElement("adjustmentReason");
			adjustmentReason.appendChild(doc
					.createTextNode(getAdjustmentReason()));
			sale.appendChild(adjustmentReason);

			// adjustmentAmount element
			Element adjustmentAmount = doc.createElement("adjustmentAmount");
			adjustmentAmount.appendChild(doc
					.createTextNode(getAdjustmentAmount()+""));
			sale.appendChild(adjustmentAmount);

			// comments element
			Element comments = doc.createElement("comments");
			comments.appendChild(doc.createTextNode(getComments()));
			sale.appendChild(comments);

			
			// productCode element
			Element productCode = doc.createElement("productCode");
			productCode.appendChild(doc.createTextNode(sp.getProductCode()));
			saleProducts.appendChild(productCode);

			// transferType element
			Element transferType = doc.createElement("transferType");
			transferType.appendChild(doc.createTextNode(sp.getTransferType()));
			saleProducts.appendChild(transferType);

			// crates element
			Element crates = doc.createElement("crates");
			crates.appendChild(doc.createTextNode(sp.getCrates()));
			saleProducts.appendChild(crates);

			// bottles element
			Element bottles = doc.createElement("bottles");
			bottles.appendChild(doc.createTextNode(sp.getBottles()));
			saleProducts.appendChild(bottles);

			// rate element
			Element rate = doc.createElement("rate");
			rate.appendChild(doc.createTextNode(sp.getRate()));
			saleProducts.appendChild(rate);

			// saleProductsTotal element
			Element saleProductsTotal = doc.createElement("saleProductsTotal");
			saleProductsTotal.appendChild(doc
					.createTextNode(sp.getSaleProductsTotal()));
			saleProducts.appendChild(saleProductsTotal);
		
		} catch (ParserConfigurationException pce) {
			pce.printStackTrace();
		}

		return doc;
	}


	public ArrayList<SaleProducts> getSaleProducts() {
		return saleProducts;
	}


	public void setSaleProducts(ArrayList<SaleProducts> saleProducts) {
		this.saleProducts = saleProducts;
	}
	
	

}
